<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.llsd
 */

require_once 'MLLSD.class.php';

/**
 * LLSD client, allow send and receive data using llsd 
 */
class MLLSDClient {

    /**
     * Curl client
     *
     * @var resource
     */
    private $curl;

    /**
     * Create a LSSD client
     */
    public function MLLSDClient() {
        // Init curl
        $this->curl = curl_init();

        // Set options
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 120);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($this->curl, CURLOPT_POST, true);
    }

    /**
     * Post the data given
     *
     * @param string $url Server url
     * @param string $data  Data to send
     * @param boolean $raw in raw format, default false
     * 
     * @return array with the response 
     */
    private function postData($url, $data, $raw = false) {
        // Create headers
        $header = array();

        // Add content type 
        if(!$raw) {
            $header = array('Content-type: application/llsd+xml');
        } else {
            $header = array('Content-type: application/octet-stream');
        }

        // Set options 
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);

        // Get the response
        $response = curl_exec($this->curl);

        // Check if valid
        if(curl_exec($this->curl) === false) {
            throw new Exception(curl_error($this->curl));
        }

        // Return the array 
        return MLLSD::decode($response);
    }

    /**
     * Post the data given in raw form 
     *
     * @param string $url Server url
     * @param string $data  Data to send
     * @return array with the response
     */
    public function postRawData($url, $data) {
        return $this->postData($url, $data, true);
    }

    /**
     * Send LLSD Request
     *
     * @param string $url Server address
     * @param array llsd request
     *
     * @return array llsd response 
     */
    public function sendRequest($url, array $llsd) {
        // Conver to xml request 
        $request = MLLSD::encode($llsd);

        // Get the response
        $response = $this->postData($url, $request);

        // Return the llsd decode 
        return $response;
    }
}

?>
